﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:administrator
	/// </summary>
	public partial class administrator
	{
		public administrator()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_administrator"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_administrator");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.administrator model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_administrator(");
			strSql.Append("user_name,user_pwd,real_name,email,user_type,user_level,is_lock)");
			strSql.Append(" values (");
			strSql.Append("@user_name,@user_pwd,@real_name,@email,@user_type,@user_level,@is_lock)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@user_name", DbType.String),
					new SQLiteParameter("@user_pwd", DbType.String),
					new SQLiteParameter("@real_name", DbType.String),
					new SQLiteParameter("@email", DbType.String),
					new SQLiteParameter("@user_type", DbType.Int32,8),
					new SQLiteParameter("@user_level", DbType.String),
					new SQLiteParameter("@is_lock", DbType.Int32,8)};
			parameters[0].Value = model.user_name;
			parameters[1].Value = model.user_pwd;
			parameters[2].Value = model.real_name;
			parameters[3].Value = model.email;
			parameters[4].Value = model.user_type;
			parameters[5].Value = model.user_level;
			parameters[6].Value = model.is_lock;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.administrator model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_administrator set ");
			strSql.Append("user_name=@user_name,");
			strSql.Append("user_pwd=@user_pwd,");
			strSql.Append("real_name=@real_name,");
			strSql.Append("email=@email,");
			strSql.Append("user_type=@user_type,");
			strSql.Append("user_level=@user_level,");
			strSql.Append("is_lock=@is_lock");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@user_name", DbType.String),
					new SQLiteParameter("@user_pwd", DbType.String),
					new SQLiteParameter("@real_name", DbType.String),
					new SQLiteParameter("@email", DbType.String),
					new SQLiteParameter("@user_type", DbType.Int32,8),
					new SQLiteParameter("@user_level", DbType.String),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.user_name;
			parameters[1].Value = model.user_pwd;
			parameters[2].Value = model.real_name;
			parameters[3].Value = model.email;
			parameters[4].Value = model.user_type;
			parameters[5].Value = model.user_level;
			parameters[6].Value = model.is_lock;
			parameters[7].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_administrator ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_administrator ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.administrator GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,user_name,user_pwd,real_name,email,user_type,user_level,is_lock from he_administrator ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.administrator model=new He.Model.administrator();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["user_name"]!=null && ds.Tables[0].Rows[0]["user_name"].ToString()!="")
				{
					model.user_name=ds.Tables[0].Rows[0]["user_name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["user_pwd"]!=null && ds.Tables[0].Rows[0]["user_pwd"].ToString()!="")
				{
					model.user_pwd=ds.Tables[0].Rows[0]["user_pwd"].ToString();
				}
				if(ds.Tables[0].Rows[0]["real_name"]!=null && ds.Tables[0].Rows[0]["real_name"].ToString()!="")
				{
					model.real_name=ds.Tables[0].Rows[0]["real_name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["email"]!=null && ds.Tables[0].Rows[0]["email"].ToString()!="")
				{
					model.email=ds.Tables[0].Rows[0]["email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["user_type"]!=null && ds.Tables[0].Rows[0]["user_type"].ToString()!="")
				{
					model.user_type=int.Parse(ds.Tables[0].Rows[0]["user_type"].ToString());
				}
				if(ds.Tables[0].Rows[0]["user_level"]!=null && ds.Tables[0].Rows[0]["user_level"].ToString()!="")
				{
					model.user_level=ds.Tables[0].Rows[0]["user_level"].ToString();
				}
				if(ds.Tables[0].Rows[0]["is_lock"]!=null && ds.Tables[0].Rows[0]["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,user_name,user_pwd,real_name,email,user_type,user_level,is_lock ");
			strSql.Append(" FROM he_administrator ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_administrator ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_administrator";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

